/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/7/5 10:07
 */
// 二叉树的直径
    // https://leetcode.cn/problems/diameter-of-binary-tree/description/
public class Solution543 {
    private int res;
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return res;
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return -1;
        }
        int lLen = dfs(root.left) + 1;
        int rLen = dfs(root.right) + 1;
        res = Math.max(res, rLen + lLen);
        return Math.max(rLen, lLen);
    }
}
